﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapRoom
{
    class XBeeMessage
    {
        private string _robotID;
        private int _messageType;
        private int _modeOfRobot;
        private double _forwardMeasurement;
        private double _starboardMeasurement;
        private double _approximateAngle;

        public double forwardMeasurement
        {
            get { return _forwardMeasurement; }
            set { _forwardMeasurement = value; }
        }

        public double starboardMeasurement
        {
            get { return _starboardMeasurement; }
            set { _starboardMeasurement = value; }
        }

        public double approximateAngle
        {
            get { return _approximateAngle; }
            set { _approximateAngle = value; }
        }
        
        public int modeOfRobot
        {
            get { return _modeOfRobot; }
            set { _modeOfRobot = value; }
        }

        public int messageType
        {
            get { return _messageType; }
            set { _messageType = value; }
        }

        public string robotID
        {
            get { return _robotID; }
            set { _robotID = value; }
        }

        public string toString()
        {
            // TODO: actually format based on messageType
            return String.Format( "START {0} {1} {2} STOP", 
                robotID, messageType, modeOfRobot );
        }

        public bool parseString( String toParse )
        {
            string [ ] splitString = toParse.Split( new Char[ ]{' '} );
            string newRobotID;
            int newMessageType;
            int newModeOfRobot;
            double newForwardMeasurement;
            double newStarboardMeasurement;
            double newApproximateAngle;

            if( splitString.Length < 4 || !splitString[ 0 ].Equals( "START" ) || 
                !splitString[ splitString.Length - 1 ].Equals( "STOP" ) )
            {
                return false;
            }

            newRobotID = splitString[ 1 ];
            
            if( !Int32.TryParse( splitString[ 2 ], out newMessageType ) )
            {
                return false;
            }

            // TODO: check message type, re-evaluate splitString length
            // based on message type.  this check is temporary
            if( newMessageType != 2 || splitString.Length < 7 )
            {
                return false;
            }

            if( !Int32.TryParse( splitString[ 3 ], out newModeOfRobot ) )
            {
                return false;
            }
            
            if( !Double.TryParse( splitString[ 4 ], out newApproximateAngle ) )
            {
                return false;
            }

            if( !Double.TryParse( splitString[ 5 ], out newForwardMeasurement ) )
            {
                return false;
            }

            if( !Double.TryParse( splitString[ 6 ], out newStarboardMeasurement ) )
            {
                return false;
            }
            
            robotID = newRobotID;
            messageType = newMessageType;
            modeOfRobot = newModeOfRobot;
            forwardMeasurement = newForwardMeasurement;
            _starboardMeasurement = newStarboardMeasurement;
            _approximateAngle = newApproximateAngle;

            return true;
        }
    }
}
